<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="人员名称" prop="employeeId">
        <el-select v-model="queryParams.employeeId" clearable filterable placeholder="请搜索选择人员名称" style="width: 217px">
          <el-option
            v-for="dict in personnel"
            :key="dict.employeeId"
            :label="dict.name"
            :value="dict.employeeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="trainList" @selection-change="handleSelectionChange">
      <el-table-column align="center" label="培训开始时间" prop="trainStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.trainStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="培训结束时间" prop="trainEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.trainEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="人员名称" prop="name" />
      <el-table-column align="center" label="身份证号" prop="certCode" />
      <el-table-column align="center" label="培训时长" prop="trainHours" />
      <el-table-column :show-overflow-tooltip="true" align="center" label="培训结果" prop="trainResult" />
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleexamine(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改人员培训对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item label="姓名" prop="employeeId">
          <el-select
            v-model="form.employeeId"
            :disabled="disabledon"
            filterable
            placeholder="请搜索选择姓名"
            style="width: 350px"
          >
            <el-option
              v-for="dict in personnel"
              :key="dict.employeeId"
              :label="dict.name"
              :value="dict.employeeId"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="培训开始时间" prop="trainStartTime">
          <el-date-picker
            v-model="form.trainStartTime"
            :disabled="disabledon"
            clearable
            placeholder="请选择培训开始时间"
            style="width: 350px"
            type="date"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="培训结束时间" prop="trainEndTime">
          <el-date-picker
            v-model="form.trainEndTime"
            :disabled="disabledon"
            clearable
            placeholder="请选择培训结束时间"
            style="width: 350px"
            type="date"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="培训时长(小时)" prop="trainHours">
          <el-input-number v-model="form.trainHours" :disabled="disabledon" :min="0" :precision="2" :step="0.1" style="width: 350px" />
        </el-form-item>
        <el-form-item label="培训结果" prop="trainResult">
          <el-input
            v-model="form.trainResult"
            :autosize="{ minRows: 2, maxRows: 4}"
            :disabled="disabledon"
            placeholder="请输入培训结果"
            style="width: 350px"
            type="textarea"
          />
        </el-form-item>
        <el-form-item label="培训详情" prop="trainDetails">
          <el-input
            v-model="form.trainDetails"
            :autosize="{ minRows: 2, maxRows: 4}"
            :disabled="disabledon"
            placeholder="请输入培训详情"
            style="width: 350px"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="disabledon == false" type="primary" v-no-more-click @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { listTrain, getTrain, delTrain, addTrain, updateTrain } from '@/api/bhxq/train'
// import { getpersonnel } from '@/api/bhxq/aptitude'
import { parseTime } from '../../../utils/ruoyi'
import { addTrain, delTrain, getpersonnel, getTrain, listTrain, updateTrain } from '@/api/informationReport'

export default {
  name: 'Index',
  dicts: ['sys_user_sex'],
  data() {
    return {
      personnel: [],
      disabledon: true,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员培训表格数据
      trainList: [{}],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        employeeId: null
      },
      queryParamslist: {
        employeeId: null,
        businessType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        enterpriseId: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        businessType: [
          { required: true, message: '类型不能为空', trigger: 'change' }
        ],
        trainStatus: [
          { required: true, message: '培训状态不能为空', trigger: 'change' }
        ],
        trainName: [
          { required: true, message: '培训名称不能为空', trigger: 'blur' }
        ],
        employeeId: [
          { required: true, message: '姓名不能为空', trigger: 'change' }
        ],
        trainStartTime: [
          { required: true, message: '培训开始时间不能为空', trigger: 'blur' }
        ],
        trainEndTime: [
          { required: true, message: '培训结束时间不能为空', trigger: 'blur' }
        ]
        // trainDetails: [
        //   { required: true, message: '培训详情不能为空', trigger: 'blur' }
        // ],
        // trainHours: [
        //   { required: true, message: '培训时长不能为空', trigger: 'blur' }
        // ],
        // trainResult: [
        //   { required: true, message: '培训结果不能为空', trigger: 'blur' }
        // ]
      }
    }
  },
  created() {
    this.getList()
    this.getpersonnellist()
  },
  methods: {
    parseTime,
    // 查询人员列表
    getpersonnellist() {
      this.queryParamslist.businessType = 1
      getpersonnel(this.queryParamslist).then(response => {
        this.personnel = response.data
      })
    },
    /** 查询人员培训列表 */
    getList() {
      // this.loading = true
      this.queryParams.businessType = 1
      listTrain(this.queryParams).then(response => {
        this.trainList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseId: null,
        businessType: null,
        trainStatus: null,
        trainName: null,
        employeeId: null,
        trainStartTime: null,
        trainEndTime: null,
        trainDetails: null,
        trainHours: null,
        trainResult: null,
        postStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.disabledon = false
      this.open = true
      this.title = '新增培训'
    },
    /** 查看按钮操作 */
    handleexamine(row) {
      this.reset()
      const id = row.id || this.ids
      getTrain(id).then(response => {
        this.form = response.data
        this.disabledon = true
        this.open = true
        this.title = '查看培训'
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getTrain(id).then(response => {
        this.form = response.data
        this.disabledon = false
        this.open = true
        this.title = '修改培训'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.businessType = 1
            updateTrain(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            this.form.businessType = 1
            addTrain(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除人员名称为"' + row.name + '"的数据项？').then(function() {
        return delTrain(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/train/export', {
        ...this.queryParams
      }, `train_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style>
.el-checkbox__input.is-disabled + .el-checkbox__label {
  color: #606266 !important;
}

.el-checkbox__input.is-disabled.is-checked + .el-checkbox__label {
  color: #606266 !important;
}

.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner:after {
  border-color: #606266 !important;
}

.el-radio__input.is-disabled + span.el-radio__label {
  color: #606266 !important;
}

.el-radio__input.is-disabled.is-checked .el-radio__inner::after {
  background-color: #606266 !important;
}

.el-radio__input.is-disabled .el-radio__inner, .el-radio__input.is-disabled.is-checked .el-radio__inner {
  border-color: #606266 !important;
}

.el-input.is-disabled .el-input__inner, textarea:disabled {
  color: #606266 !important;
}

.el-range-editor.is-disabled input {
  color: #606266 !important;
}

.el-tooltip__popper {

  max-width: 20%;

}

.el-tooltip__popper, .el-tooltip__popper.is-dark {

  background: rgb(48, 65, 86) !important;

  color: #fff !important;

  line-height: 24px;

}
</style>
